package com.linkage.educloud.ah.mvp.presenter;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.MessItem;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.data.Ws;
import com.linkage.educloud.ah.mvp.contract.IMessageContract;
import com.linkage.educloud.ah.mvp.model.MessageModel;

import java.util.ArrayList;
import java.util.List;

/**
 * <desc>
 * Created by The Moss on 2018/10/8.
 */

public class MessagePresenter extends IMessageContract.IPresenter {
    IMessageContract.IModel iModel;

    public MessagePresenter(IMessageContract.IView iView) {
        super(iView);
        iModel = new MessageModel();
    }

    @Override
    public void loadDatasFromServer() {
//        Observable observable = iModel.request(Method.GET, true, RequestUtil.getMessageInfo());
//        observable.compose(iView.bindLifecycle())
//                .subscribe(new BaseSubscriber<String>() {
//                    @Override
//                    public void onNext(String s) {
//                        try {
//                            JSONObject json = new JSONObject(s);
//                            if (json.getInt("ret") == Constants.Result.ret) {
//                                List<MessItem> mess = MessItem.fromJSONObject(json);
//                            } else {
//
//                            }
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                    @Override
//                    public void onError(ApiException e) {
//                        ((IMessageContract.IView) iView).onLoadDatasFromServer(false, null);
//                    }
//                });
        List<MessItem> messlist = testData();
        //删除配置项无用配置
        StringBuffer ids = new StringBuffer();
        for (int i = 0; i < messlist.size(); i++) {
            if (i == messlist.size() - 1)
                ids.append(messlist.get(i).getId());
            else
                ids.append(messlist.get(i).getId() + ",");
        }
        MyApplication.getInstance().getResovler().deleteNoUseDataFromThread(AccountConfig.getLoginAccount().getLoginAccount(), ids.toString());
        //更新本地配置
        MyApplication.getInstance().getResovler().insertMessageOrderServer(AccountConfig.getLoginAccount().getLoginAccount(), messlist);
        ((IMessageContract.IView) iView).onLoadDatasFromServer(true, null);
    }

    private List<MessItem> testData() {
        List<MessItem> items = new ArrayList<MessItem>();
        try {
            MessItem dysic = new MessItem();
            dysic.setId(Constants.MESS_TYPE.APP_ID_SP + "76676");
            dysic.setTitle("班级空间");
            dysic.setContent("测试一班");
            dysic.setType(Ws.MessageType.TYPE_CLASS_SPACE);
            dysic.setThreadType(Constants.ThreadType.THREAD_1);
            dysic.setChatType(Constants.ChatType.CHAT_TYPE_OTHER);
            dysic.setIcon(R.mipmap.campus_space);
            items.add(dysic);
            MessItem topic = new MessItem();
            topic.setId(Constants.MESS_TYPE.APP_ID_TOPIC + " 987783");
            topic.setTitle("话题圈");
            topic.setContent("话题话题怀特");
            topic.setType(Ws.MessageType.TYPE_TOPIC);
            topic.setThreadType(Constants.ThreadType.THREAD_2);
            topic.setChatType(Constants.ChatType.CHAT_TYPE_OTHER);
            topic.setIcon(R.mipmap.circleicons);
            items.add(topic);
            MessItem clazz = new MessItem();
            clazz.setId("123434");
            clazz.setTitle("班级群聊");
            clazz.setContent("测试一班");
            clazz.setType(Ws.MessageType.TYPE_MSG_TEXT);
            clazz.setThreadType(Constants.ThreadType.THREAD_IM_CLASS);
            clazz.setChatType(Constants.ChatType.CHAT_TYPE_GROUP);
            clazz.setIcon(R.mipmap.group_avatar);
            items.add(clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return items;
    }
}
